﻿using RMS.BLL;
using RMS.Common;
using RMS.EF.Model;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Web.Security;

namespace RMS.Manage.Web.Controllers
{
    public class BaseController : Controller
    {
        public BaseController()
        {
            ViewBag.Title = GetRMSConfigByKey("RMSTitle");
        }

        NLogHelper nlog = new NLogHelper();
        /// <summary>
        /// 当前登录用户的Id
        /// </summary>
        protected static string UserId
        {
            get
            {
                return System.Web.HttpContext.Current.User.Identity.Name; 
            }
        }
        /// <summary>
        /// 当前是否登陆
        /// </summary>
        protected static bool IsLogin
        {
            get
            {
                return System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }
        /// <summary>
        /// 登陆的用户信息
        /// </summary>
        protected static RMS_User LoginUser
        {
            get
            {
                RMS_User result = new RMS_User();
                if (IsLogin && System.Web.HttpContext.Current.User.Identity is FormsIdentity)
                {
                    FormsIdentity iden = (System.Web.HttpContext.Current.User.Identity) as FormsIdentity;
                    if (iden != null)
                    {
                        FormsAuthenticationTicket ticket = iden.Ticket;
                        if (!string.IsNullOrWhiteSpace(ticket.UserData))
                        {
                            result = JsonConvert.DeserializeObject<RMS_User>(ticket.UserData);
                        }
                    }
                }
                return result;
            }
        }
        /// <summary>
        /// 当前项目的项目Id
        /// </summary>
        public static string ProgramId { get { return "1"; } }
        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProgramName
        {
            get
            {
                return GetRMSConfigByKey("RMSTitle");
            }
        }

        private static List<RMS_Menu> menu;
        /// <summary>
        /// 有权限的菜单
        /// </summary>
        public static List<RMS_Menu> Menu
        {
            get
            {
                if (string.IsNullOrWhiteSpace(UserId) || ProgramId != "1")
                {
                    return new List<RMS_Menu>();
                }
                if (menu == null || menu.Count <= 0)
                {
                    MenuBll mbll = new MenuBll();
                    var result_m = mbll.GetMenuByManagerId(UserId, ProgramId);
                    if (!result_m.IsSuccess)
                    {
                        result_m.Data = new List<RMS_Menu>();
                    }
                    List<RMS_Menu> mlist = result_m.Data;
                    menu = new List<RMS_Menu>();
                    //menu.Add(new RMS_Menu()
                    //{
                    //    Id = -1,
                    //    ParentId = 0,
                    //    Name = "Home",
                    //    Url = "/Home/Index",
                    //    Type = 1,
                    //    Ordering = 0,
                    //    Deleted = 0,
                    //    Status = 1
                    //});
                    menu.AddRange(mlist);
                }
                return menu;
            }
            set
            {
                menu = value;
            }
        }
        /// <summary>
        /// 获取配置参数
        /// </summary>
        /// <param name="key">配置键</param>
        /// <returns></returns>
        protected string GetRMSConfigByKey(string key)
        {
            string result = string.Empty;
            ConfigBll cbll = new ConfigBll();
            var result_c= cbll.GetEnableByKey(key);
            if (!result_c.IsSuccess)
            {
                nlog.Error("GetRMSConfigByKey_ERROR:" + result_c.Message);
            }
            result = result_c.Data;
            return result;
        }

        #region datatables分页用设置方法
        /// <summary>
        /// 列表页获取分页参数,无筛选条件
        /// </summary>
        /// <returns></returns>
        public PageParams SetPageParams()
        {
            PageParams p = new PageParams();
            p.Draw = Request.Form["draw"].ToInt();
            p.Start = Request.Form["start"].ToInt();
            p.Length = Request.Form["length"].ToInt();
            int ordercoli = Request.Form["order[0][column]"].ToInt();
            p.Order = Request.Form["columns[" + ordercoli + "][data]"];
            p.OrderType = Request.Form["order[0][dir]"];
            return p;
        }
        /// <summary>
        /// 列表页获取分页参数
        /// </summary>
        /// <typeparam name="T">筛选条件类型</typeparam>
        /// <param name="sm">筛选条件</param>
        /// <returns></returns>
        public PageParamsT<T> SetPageParams<T>(T sm)
        {
            PageParamsT<T> p = new PageParamsT<T>();
            p.Draw = Request.Form["draw"].ToInt();
            p.Start = Request.Form["start"].ToInt();
            p.Length = Request.Form["length"].ToInt();
            int ordercoli = Request.Form["order[0][column]"].ToInt();
            p.Order = Request.Form["columns[" + ordercoli + "][data]"];
            p.OrderType = Request.Form["order[0][dir]"];
            p.SearchModel = sm;
            return p;
        }
        /// <summary>
        /// 设置分页用json化模型
        /// </summary>
        /// <param name="page">分页信息</param>
        /// <param name="list">列表数据</param>
        /// <returns></returns>
        public object SetPageJson(PageParams page, IEnumerable list)
        {
            return new { draw = page.Draw, recordsTotal = page.Total, recordsFiltered = page.Total, data = list };
        } 
        #endregion

        #region 错误页跳转
        /// <summary>
        /// 跳转至404页面
        /// </summary>
        /// <returns></returns>
        public ActionResult RedirectTo404()
        {
            return RedirectToAction("_404", "Error", new { area = "" });
        }
        /// <summary>
        /// 跳转至Error页面
        /// </summary>
        /// <returns></returns>
        public ActionResult RedirectToError()
        {
            return RedirectToAction("Index", "Error", new { area = "" });
        }
        #endregion
    }
}